﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-05-15 10:35:52
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class AuthorityRoleMenuRela
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个
    	/// </summary>
        public AuthorityRoleMenuRelaInfo QuerySingle(Expression<Func<AuthorityRoleMenuRelaInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取列表
    	/// </summary>
	    public IList<AuthorityRoleMenuRelaInfo> QueryList(Expression<Func<AuthorityRoleMenuRelaInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }
        /// <summary>
        /// 删除权限        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="SystemID"></param>
        public void DeleteRoleMenu(int RoleID, int SystemID)
        {
            string strSql = @"	  UPDATE [AuthorityRoleMenuRela] SET IsActive=0,UpdateTime=GETDATE()
                                  WHERE [RoleID]=@RoleID AND SystemID=@SystemID ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            //设置参数
            db.AddInParameter(dbc, "@RoleID", DbType.Int32, RoleID);
            db.AddInParameter(dbc, "@SystemID", DbType.Int32, SystemID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="SystemID"></param>
        public IList<AuthorityRoleMenuRelaInfo> GetRoleMenuBySystemIDandRoleID(int RoleID, int SystemID)
        {
            IList<AuthorityRoleMenuRelaInfo> list = new List<AuthorityRoleMenuRelaInfo>();
            string strSql = @" SELECT  *
                                  FROM [dbo].[AuthorityRoleMenuRela]
                                  WHERE [IsActive]=1 AND [RoleID]=@RoleID AND SystemID=@SystemID";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@RoleID", DbType.Int32, RoleID);
            db.AddInParameter(dbc, "@SystemID", DbType.Int32, SystemID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthorityRoleMenuRelaInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            return list;
        }
        /// <summary>
        /// 根据角色获取菜单 
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="SystemID"></param>
        public IList<AuthorityMenuInfo> GetRoleMenuList(int RoleID, int SystemID)
        {
            IList<AuthorityMenuInfo> list = new List<AuthorityMenuInfo>();
            string strSql = @" SELECT a.*
                          FROM [dbo].[AuthorityMenu] a 
                          INNER JOIN [AuthorityRoleMenuRela] b
                          ON a.MenuID=b.MenuID 
                          WHERE a.[IsActive]=1 and b.isActive=1 AND a.SystemID=@SystemID  and b.RoleID=@RoleID
                          ORDER BY [OrderNum]  ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@RoleID", DbType.Int32, RoleID);
            db.AddInParameter(dbc, "@SystemID", DbType.Int32, SystemID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthorityMenuInfo info = SQLServerProvider.Create<AuthorityMenuInfo>(dr);
                    list.Add(info);
                }
            }
            return list;
        }
    }
	
}